package com.zfxt.biz.service.impl;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/9/6.
 */
@Service
public class JedisServiceImpl {
    private static final String HOT_HOUSE_KEY="hot_house";
    private static final Logger logger= LoggerFactory.getLogger(JedisServiceImpl.class);

    public void increase(int id){
        Jedis jedis=new Jedis("58.87.117.154",7007);
        jedis.auth("123456");
        jedis.zincrby(HOT_HOUSE_KEY,1.0D,id+"");
        jedis.zremrangeByRank(HOT_HOUSE_KEY,0,-11);
        jedis.close();
    }
    public List<Long> getHot() {
        try {
            Jedis jedis=new Jedis("58.87.117.154",7007);
            jedis.auth("123456");
            Set<String> idSet = jedis.zrevrange(HOT_HOUSE_KEY, 0, -1);
            jedis.close();
            List<Long> ids = idSet.stream().map(Long::parseLong).collect(Collectors.toList());
            return ids;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);//有同学反应在未安装redis时会报500,在这里做下兼容,
            return Lists.newArrayList();
        }
    }

    public List<String> getHotHouse(Integer size) {
        List<String> list = new ArrayList<String>();
            list = list.subList(0, Math.min(list.size(), size));
        if (list.isEmpty()) {
            return Lists.newArrayList();
        }
        return list;
    }
}
